package com.macrounion.atv.init;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import com.google.common.collect.Lists;
import com.macrounion.atv.cache.ChannelCache;
import com.macrounion.atv.cache.CurrentDataCache;
import com.macrounion.atv.cache.TrapRemoveCache;
import com.macrounion.atv.cache.VirtualChannelCache;
import com.macrounion.atv.processor.ChannelProcessor;
import com.macrounion.atv.processor.DeviceProcessor;
import com.macrounion.atv.service.entity.Channel;
import com.macrounion.atv.service.entity.CurrentData;
import com.macrounion.atv.service.entity.Device;
import com.macrounion.atv.service.entity.ext.VirtualExt;
import com.macrounion.atv.service.service.ChannelService;
import com.macrounion.atv.service.service.CurrentDataService;
import com.macrounion.atv.service.service.VirtualChannelService;

/**
 * @author zxj
 * createTime 2020-11-23 12:37
 **/
@Component
public class ApplicationRunnerImpl implements ApplicationRunner {
	
	@Autowired
	private DeviceProcessor deviceProcessor;
	@Autowired
	private VirtualChannelCache virtualChannelCache;
	@Autowired
	private VirtualChannelService virtualService;
	@Autowired
	private ChannelProcessor channelProcessor;
	@Autowired
	private ChannelService channelService;
    @Autowired
    private ChannelCache channelCache;
    @Autowired
    private CurrentDataCache currentDataCache;
    @Autowired
    private CurrentDataService currentDataService;
    @Autowired
    private TrapRemoveCache trapRemoveCache;
	
    @Override
    public void run(ApplicationArguments args) throws Exception {
        initDevice();
        initTrapRemoveCache();
        initVirChannel();
        initChannel();
    }

    /**
     * 初始化当前是否有trap告警
     */
    private void initTrapRemoveCache() {
		int n = currentDataService.getTrapNum();
		trapRemoveCache.saveRemoved(n == 0);
	}

	/**
     * 加载通道
     */
	private void initChannel() {
		List<Channel> all = channelService.getAll();
		if(CollectionUtils.isEmpty(all))
			return;
		all.forEach(c->{
			channelCache.saveCache(c);
		});
	}

	/**
     * 加载一次设备
     */
    public void initDevice() {
    	List<Device> ds = deviceProcessor.getAllDevice();
    	if(CollectionUtils.isEmpty(ds))
			return;
    	ds.forEach(d->{
    		initCurrentData(d);
    	});
    }

    /**
     * 加载当前值
     * @param d
     */
    private void initCurrentData(Device d) {
		List<CurrentData> datas = currentDataCache.getCurrentData(d.getId());
		if(CollectionUtils.isEmpty(datas))
			return;
		datas.forEach(data->{
			currentDataCache.saveCurrentdatadetails(data);
		});
	}

	/**
     * 加载虚拟通道
     */
    private void initVirChannel() {
		List<VirtualExt> vs = virtualService.getAll();
		if(CollectionUtils.isEmpty(vs))
			return;
		List<String> keys = Lists.newArrayList();
		vs.forEach(v->{
			List<String> ks = channelProcessor.getFullCodesByFx(v.getFx());
			if(!CollectionUtils.isEmpty(ks)) {
				ks.forEach(k->{
					if(!keys.contains(k)) {
						keys.add(k);
						try {
//							String[] codes = k.split("_");
//							System.out.println(k.substring(k.lastIndexOf("_")+1));
//							System.out.println(k.substring(0,k.lastIndexOf("_")));
							virtualChannelCache.getIncludeChannel(k.substring(0,k.lastIndexOf("_")), k.substring(k.lastIndexOf("_")+1));
						} catch (Exception e) {
							//公式不正确或存在开关量通道
						}
					}
				});
			}
		});
	}
}